//Documentation complete
/**
 * @author Jeff
 * @version 2009MR03
 */

package ui.internal.ajax;

import problemDomain.*;
import utilities.*;
import control.*;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.*;

/**
 * Servlet implementation class MaintainPatronAccount
 */
public class MaintainPatronAccount extends HttpServlet {
	
	/* Constants */
	
	private static final long serialVersionUID = -1462473538011726438L;
	
	/* END Constants */
	
	
	/* (HttpServlet) Methods */
	
	@Override
	protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(false);
		response.setContentType("text/xml");
		if (session != null) {
				String action = request.getParameter("a");
				if (action != null) {
					if (action.equalsIgnoreCase("c"))
						create(request, response);
					else if (action.equalsIgnoreCase("r"))
						report(request, response);
					else if (action.equalsIgnoreCase("u"))
						update(request, response);
					else if (action.equalsIgnoreCase("d"))
						delete(request, response);
					else if (action.equalsIgnoreCase("l"))
						listAll(response);
				} else
					response.setStatus(HttpServletResponse.SC_NO_CONTENT);
			//} else
			//	response.setStatus(HttpServletResponse.SC_NO_CONTENT);
		} else
			response.setStatus(HttpServletResponse.SC_NO_CONTENT);
	}
	
	/* END (HttpServlet) Methods */
	
	
	/* Local Methods */
	
	private void create (HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		String firstname = request.getParameter("firstname");
		String lastname = request.getParameter("lastname");
		String phone = request.getParameter("phone");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String email = request.getParameter("email");
		String street = request.getParameter("street");
		String city = request.getParameter("city");
		String province = request.getParameter("province");
		String postalcode = request.getParameter("postalcode");
		
		
		//String firstname, String lastname, String phone, String address, 
		//String city, String province, String postalcode, String email, 
		//String username, String password
		
		PrintWriter out = response.getWriter();
		try {
			System.out.println("ZZZZ in the servlet");
			PatronAccountControl pac = new PatronAccountControl();
			pac.create(firstname, lastname, phone,username, password, email, street, city, province, postalcode);
			out.write(ErrorBean.getSuccess().toXML());//"Item created"));
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}

	private void report (HttpServletRequest request, HttpServletResponse response) throws IOException {
		String firstname = request.getParameter("firstname");
		String lastname = request.getParameter("lastname");
		String phone = request.getParameter("phone");
		String username = request.getParameter("username");
		String email = request.getParameter("email");
		String city = request.getParameter("city");
		String province = request.getParameter("province");
		String postalcode = request.getParameter("postalcode");
		
		PrintWriter out = response.getWriter();
		try {
			PatronAccountControl pac = new PatronAccountControl();
			List<RegisteredPatronAccountBean> reportList = pac.search(firstname, lastname, username,phone,email,city,province,postalcode);
			out.write(XMLTools.encapsulateBeanListXML(reportList, "patrons"));
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	private void update (HttpServletRequest request, HttpServletResponse response) throws IOException {
		String id = request.getParameter("id");
		String firstname = request.getParameter("firstname");
		String lastname = request.getParameter("lastname");
		String phone = request.getParameter("phone");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String email = request.getParameter("email");
		String street = request.getParameter("street");
		String city = request.getParameter("city");
		String province = request.getParameter("province");
		String postalcode = request.getParameter("postalcode");
		
		PrintWriter out = response.getWriter();
		try {
			PatronAccountControl pac = new PatronAccountControl();
			pac.update(id, username, password, firstname, lastname, phone, email, street, city, province, postalcode);
			out.write(ErrorBean.getSuccess().toXML());
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}

	private void delete (HttpServletRequest request, HttpServletResponse response) throws IOException {
		String id = request.getParameter("id");
		PrintWriter out = response.getWriter();
		try {
			PatronAccountControl pac = new PatronAccountControl();
			pac.delete(id);
			out.write(ErrorBean.getSuccess().toXML());
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	private void listAll (HttpServletResponse response) throws IOException  {
		PrintWriter out = response.getWriter();

		try {
			PatronAccountControl pac = new PatronAccountControl();
			List<RegisteredPatronAccountBean> list = pac.listAll();
			out.write(XMLTools.encapsulateBeanListXML(list, "patrons"));
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	/* END Local Methods */
	
}
